package com.jy.app.lib.view.custom;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jy.app.lib.R;
import com.jy.app.lib.net.HttpUtil;


/**
 * Created by tm on 2016/4/22.
 */
public class LoadingDialog {
    private Context context;
    private HttpDailog dialog;
    private Display display;
    private View view;
    private UniversalLoadingView loadingView;
    private TextView tv_msg;
    private RelativeLayout rl_layout;
    private Boolean isCancelRequest = false;
    private Handler handler = new Handler();

    public LoadingDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        init();
    }

    public LoadingDialog(Context context, String text) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        init();
        setText(text);
    }

    private void init(){
        view = LayoutInflater.from(context).inflate(R.layout.dailog_loading, null);
        rl_layout = (RelativeLayout) view.findViewById(R.id.rl_layout);
        loadingView = (UniversalLoadingView) view.findViewById(R.id.loadingView);
        tv_msg = (TextView) view.findViewById(R.id.tv_msg);

        dialog = new HttpDailog(context, com.jy.app.lib.R.style.AlertDialogStyle);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        rl_layout.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.6), LinearLayout.LayoutParams.WRAP_CONTENT));
    }
    public void setBGradient(boolean isBGradient){
        loadingView.setBGradient(isBGradient);
    }

    public void setText(String msg){
        tv_msg.setText(msg);
        view.postInvalidate();
    }

    public void setTextColor(int color){
        tv_msg.setTextColor(color);
    }

    public void show(){
        isCancelRequest = false;
        if (dialog!=null&&(!dialog.isShowing()))
            dialog.show();
    }

    public void show(String message){
        isCancelRequest = false;
        setText(message);
        if (dialog!=null&&(!dialog.isShowing()))
            dialog.show();
    }

    public void delayedShow(){
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                isCancelRequest = false;
                if (dialog!=null&&(!dialog.isShowing()))
                    dialog.show();
            }
        },1000);
    }

    public void httpShow(){
        isCancelRequest = true;
        if (dialog!=null&&(!dialog.isShowing()))
            dialog.show();
    }

    public void httpShow(String message){
        isCancelRequest = true;
        setText(message);
        if (dialog!=null&&(!dialog.isShowing()))
            dialog.show();
    }

    public void dismiss(){
        if (handler != null) handler.removeCallbacks(null);
        isCancelRequest = false;
        if (dialog!=null||dialog.isShowing())
        dialog.dismiss();
    }

    public boolean isShowing(){
        return dialog.isShowing();
    }

    public void setBackgroundColor(int color){
        rl_layout.setBackgroundColor(color);
    }
    public void setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
    }
    class HttpDailog extends Dialog{

        public HttpDailog(@NonNull Context context) {
            super(context);
        }

        public HttpDailog(@NonNull Context context, @StyleRes int themeResId) {
            super(context, themeResId);
        }

        protected HttpDailog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
        }

        @Override
        public void dismiss() {
            if (isCancelRequest)
            HttpUtil.cancelRequest();
            try {
                super.dismiss();
            }catch (Exception e){
            }
        }
    }
}


